package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.entity.ShippingOrder;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.service.ShippingOrderService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("shippingOrderAction")
@Scope("prototype")
public class ShippingOrderAction extends BaseAction implements ModelDriven<ShippingOrder>  {

    @Autowired
    private ShippingOrderService shippingOrderService;

    @Autowired
    private PackingListService packingListService;
    private Pagination page=new Pagination();

    private List<ShippingOrder> shippingOrderList;

    private ShippingOrder model= new ShippingOrder();

    public String list(){

    Pagination pagination =  shippingOrderService.findPage(page);
    pagination.setUrl("shippingOrderAction_list");
    push(pagination);
    return "list";
    }

    public String tocreate(){

        //查询全部状态为1的装箱单
        //fixme  需要查询状态为1 的装箱表
        Pagination pagination =  packingListService.findPage(page);
        pagination.setUrl("packingListAction_tocreate");
        push(pagination);
    return "tocreate";
    }

    public String insert(){
        shippingOrderService.save(model);
        return list();
    }

    public String toupdate(){
    ShippingOrder shippingOrder = shippingOrderService.findById(model.getId());
    super.push(shippingOrder);

    shippingOrderList = shippingOrderService.findAll();
    shippingOrderList.remove(shippingOrder);
    return "toupdate";
    }

    public String update(){
    shippingOrderService.update(model);
    return list();
    }

    /**
    * @return
    */
    public String delete(){
        String[] ids = model.getId().split(", ");
        for (String id:ids) {
            shippingOrderService.deleteById(model.getId());
        }

         return list();
    }

    /**
     * 提交委托单-》发票（one to one）
     * @return
     */
    public String toInvoice(){
        String id = model.getId();
        shippingOrderService.updateStateById(1,id);
        return "rlist";
    }
    /**
     * 取消上报委托单
     * @return
     */
    public String toCancel(){
        String id = model.getId();
        shippingOrderService.updateStateById(0,id);
        return "rlist";
    }
    public String toview()
    {
    ShippingOrder shippingOrder = shippingOrderService.findById(model.getId());
    push(shippingOrder);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<ShippingOrder> getShippingOrderList() {
    return shippingOrderList;
    }

    @Override
    public ShippingOrder getModel() {
    return model;
    }
}
